// pages/orderDetail/index.js
var util = require('../../utils/util.js');
var WxParse = require('../../utils/wxParse/wxParse.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: ['《全心体验线路订购协议》', '《线路服务协议》'],
    result: ['《全心体验线路订购协议》', '《线路服务协议》'],
    resData: '',
    show: false,
    title: '',
    content: '',
    show2: false,
    protocol: '',
    tradingAgreement: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    util.http({
      url: 'api/miniwechat/order/find_order',
      data: {
        order_id: options.id,
      }
    }).then(res => {
      WxParse.wxParse('routerAgree', 'html', res.protocol, that, 5)
      res.name = res.contacts.split("_")[0]
      res.phone = res.contacts.split("_")[1]
      res.line_time.begin_time = res.line_time.begin_time.split(" ")[0]
      res.line_time.end_time = res.line_time.end_time.split(" ")[0]
      console.log(res)
      that.setData({
        resData: res,
        protocol: res.protocol
      })
    }).catch(ex => {
      setTimeout(function () {
        wx.hideLoading()
      }, 500)
    })

    util.http({
      url: 'api/miniwechat/user/service_agree'
    }).then(res => {
      WxParse.wxParse('orderAgree', 'html', res.trading_agreement, that, 5)
    }).catch(err => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
  },

  onPopup (e) {
    let data = e.currentTarget.dataset
    if (data.index ==0 ) {
      this.setData({
        show: true,
      })
    } else {
      this.setData({
        show2: true,
      })
    }
  },

  onClose () {
    this.setData({
      show: false
    })
  },
  onClose2() {
    this.setData({
      show2: false
    })
  },
  goPaid(e) {
    console.log(e)
    let data = e.currentTarget.dataset;
    if (data.people > 0) {
      util.http({
        url: 'api/miniwechat/order/pay',
        method: 'POST',
        isPay: true,
        data: {
          out_trade_no: e.currentTarget.dataset.num,
          source: 2
        }
      }).then(res => {
        wx.requestPayment({
          timeStamp: String(res.timeStamp),
          nonceStr: String(res.nonceStr),
          package: String(res.package),
          signType: String(res.signType),
          paySign: String(res.paySign),
          success(resx) {
            wx.redirectTo({
              url: '/pages/paySuccess/index?state=1' + '&orderId=' + e.currentTarget.dataset.orderid
            })
          },
          fail(res) {
            wx.redirectTo({
              url: '/pages/paySuccess/index?state=0'
            })
          }
        })
      })
    } else {
      wx.setStorage({
        key: 'orderInfo',
        data: {
          user_id: util.isLogin().user_id,
          line_id: data.id,
          line_time_id: data.timeid,
          out_trade_no: data.num
        }
      })
      wx.navigateTo({
        url: '/pages/firmOrder/index?source=2',
      })
    }
  },
  toRefund(e) {
    wx.navigateTo({
      url: '/pages/applyRefund/index?id=' + e.currentTarget.dataset.id,
    })
  },
  refundDetail(e) {
    wx.navigateTo({
      url: '/pages/refund/index?id=' + e.currentTarget.dataset.id
    })
  },
  toComplete(e) {
    let orderId = e.currentTarget.dataset.id
    let time = e.currentTarget.dataset.time ? new Date(e.currentTarget.dataset.time) : '';
    let that = this;
    console.log(time)
    if (new Date() < time) {
      wx.showModal({
        title: '',
        content: '线路未出行完毕，点击“确认”后，款项将支付给商家，请确定',
        confirmColor: '#E50164',
        success(res) {
          if (res.confirm) {
            util.http({
              url: 'api/miniwechat/order/order_ful',
              method: 'POST',
              data: {
                user_id: util.isLogin().id,
                order_id: orderId,
              }
            }).then(res => {
              that.init();
              wx.showToast({
                title: '确认成功',
                icon: 'none',
                duration: 2000
              })
            }).catch(err => {
              wx.showToast({
                title: '确认失败',
                icon: 'none',
                duration: 2000
              })
            })
          } else if (res.cancel) {
            console.log('取消')
          }
        }
      })
      return;
    }
    wx.showModal({
      title: '',
      content: '确认已出行？',
      confirmColor: '#E50164',
      success(res) {
        if (res.confirm) {
          util.http({
            url: 'api/miniwechat/order/order_ful',
            method: 'POST',
            data: {
              user_id: util.isLogin().id,
              order_id: orderId,
            }
          }).then(res => {
            that.init();
            wx.showToast({
              title: '确认成功',
              icon: 'none',
              duration: 2000
            })
          }).catch(err => {
            wx.showToast({
              title: '确认失败',
              icon: 'none',
              duration: 2000
            })
          })
        } else if (res.cancel) {
          console.log('取消')
        }
      }
    })
  },
  toComment(e) {
    wx.navigateTo({
      url: '/pages/comment/index?lineId=' + e.currentTarget.dataset.lineid + '&orderId=' + e.currentTarget.dataset.orderid,
    })
  }

})